<?php
/**
 * Created by PhpStorm.
 * User: macmini
 * Date: 2018/3/1
 * Time: 上午11:17
 */

namespace app\common\controller;

use think\controller\Rest;

class Api extends Rest {
    protected function responseSend($user_id, $data=[], $code = 200, $type = 'json')
    {
        if (!is_array($data)) {
            $data = [$data];
        }
        $msg = [
            'data' => $data,
            'status' => $code,
        ];
        if ($user_id) {
            $res = ['user_id' => $user_id];
            $msg['token'] = base64_encrypt(json_encode($res));
        }
        return $this->response($msg);
    }

    protected function getUserId($token)
    {
        if (!$token) {
            return 300;
        }
        $res = json_decode(base64_decrypt($token), true);
        if (!is_array($res)) {
            return 300;
        }
        return $res;
    }
}
